<template>
  <div class="mobile-dashboard">
    <!-- 欢迎卡片 -->
    <div class="welcome-card">
      <div class="welcome-content">
        <h2 class="welcome-title">欢迎回来</h2>
        <p class="welcome-subtitle">{{ getCurrentTime() }}</p>
      </div>
      <div class="welcome-avatar">
        <el-avatar :size="50">
          <el-icon><User /></el-icon>
        </el-avatar>
      </div>
    </div>

    <!-- 系统统计概览 -->
    <div class="stats-overview">
      <el-row :gutter="20">
        <el-col :span="6">
          <el-card class="stats-card">
            <div class="stats-item">
              <el-icon class="stats-icon primary"><Setting /></el-icon>
              <div class="stats-content">
                <div class="stats-value">{{ systemStats.totalParams }}</div>
                <div class="stats-label">系统参数</div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card class="stats-card">
            <div class="stats-item">
              <el-icon class="stats-icon success"><DataAnalysis /></el-icon>
              <div class="stats-content">
                <div class="stats-value">{{ systemStats.dataRecords }}</div>
                <div class="stats-label">数据记录</div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card class="stats-card">
            <div class="stats-item">
              <el-icon class="stats-icon warning"><Warning /></el-icon>
              <div class="stats-content">
                <div class="stats-value">{{ systemStats.anomalies }}</div>
                <div class="stats-label">异常监测</div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card class="stats-card">
            <div class="stats-item">
              <el-icon class="stats-icon info"><User /></el-icon>
              <div class="stats-content">
                <div class="stats-value">{{ systemStats.activeUsers }}</div>
                <div class="stats-label">在线用户</div>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>

    <!-- 系统运行状态 -->
    <div class="system-status">
      <h2 class="section-title">
        <el-icon><CircleCheck /></el-icon>
        系统运行状态
      </h2>
      <el-row :gutter="24">
        <el-col :span="8">
          <el-card class="status-card">
            <div class="status-item">
              <div class="status-icon success">
                <el-icon><CircleCheck /></el-icon>
              </div>
              <div class="status-content">
                <h3>智能参数引擎</h3>
                <p class="status-text success">运行正常</p>
                <div class="status-metrics">
                  <span>响应时间: 45ms</span>
                  <span>内存使用: 12.5%</span>
                </div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="8">
          <el-card class="status-card">
            <div class="status-item">
              <div class="status-icon info">
                <el-icon><Warning /></el-icon>
              </div>
              <div class="status-content">
                <h3>数据库连接</h3>
                <p class="status-text success">连接正常</p>
                <div class="status-metrics">
                  <span>连接池: 8/20</span>
                  <span>查询QPS: 125</span>
                </div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="8">
          <el-card class="status-card">
            <div class="status-item">
              <div class="status-icon warning">
                <el-icon><Warning /></el-icon>
              </div>
              <div class="status-content">
                <h3>Redis缓存</h3>
                <p class="status-text success">运行正常</p>
                <div class="status-metrics">
                  <span>命中率: {{ redisStats.valueHitRate || '0%' }}</span>
                  <span>最近更新: {{ redisStats.lastUpdate || '-' }}</span>
                </div>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>

    <!-- 快速操作区域 -->
    <div class="quick-actions">
      <h2 class="section-title">
        <el-icon><Lightning /></el-icon>
        快速操作
      </h2>
      <el-row :gutter="20">
        <el-col :span="6">
          <el-button type="primary" plain size="large" @click="quickAction('add-param')" class="quick-btn">
            <el-icon><Plus /></el-icon>
            新增参数
          </el-button>
        </el-col>
        <el-col :span="6">
          <el-button type="success" plain size="large" @click="quickAction('create-rule')" class="quick-btn">
            <el-icon><Star /></el-icon>
            创建规则
          </el-button>
        </el-col>
        <el-col :span="6">
          <el-button type="warning" plain size="large" @click="quickAction('ai-recommend')" class="quick-btn">
            <el-icon><Cpu /></el-icon>
            AI推荐
          </el-button>
        </el-col>
        <el-col :span="6">
          <el-button type="info" plain size="large" @click="quickAction('system-monitor')" class="quick-btn">
            <el-icon><Monitor /></el-icon>
            系统监控
          </el-button>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getRedisStats } from '@/assets/api/dashboard'
import {
  DataAnalysis, Warning, User, CircleCheck, Lightning, Plus, Star, Monitor
} from '@element-plus/icons-vue'

const router = useRouter()

// 系统统计数据
const systemStats = reactive({
  totalParams: 156,
  dataRecords: '2.3M',
  anomalies: 23,
  activeUsers: 8
})

const redisStats = reactive({ valueHitRate: '0%', lastUpdate: '' })

// 页面挂载时获取统计数据
onMounted(() => {
  loadSystemStats()
})

// 加载系统统计数据
const loadSystemStats = async () => {
  try {
    // 这里后续可以调用API获取真实数据
    // const response = await systemAPI.getStats()
    // Object.assign(systemStats, response.data)
    
    // 模拟数据更新
    console.log('系统统计数据加载完成')

    // 获取Redis真实指标
    try {
      const r = await getRedisStats()
      Object.assign(redisStats, r.data || r)
    } catch (e) {
      console.warn('获取Redis指标失败', e)
    }
  } catch (error) {
    console.error('获取系统统计失败:', error)
  }
}

// 快速操作
const quickAction = (action) => {
  switch (action) {
    case 'add-param':
      router.push('/param-engine/definitions/create')
      break
    case 'create-rule':
      router.push('/param-engine/rules/create')
      break
    case 'ai-recommend':
      router.push('/param-engine/ai-recommendations')
      break
    case 'system-monitor':
      router.push('/param-engine/monitoring')
      break
    default:
      ElMessage.info('功能开发中，敬请期待！')
  }
}
</script>

<style scoped>
.dashboard-container {
  padding: 0;
  background: transparent;
}

/* 页面标题区域 */
.page-header {
  margin-bottom: 24px;
}

.page-title {
  font-size: 24px;
  font-weight: bold;
  color: #303133;
  margin: 0 0 8px 0;
  display: flex;
  align-items: center;
  gap: 12px;
}

.page-subtitle {
  font-size: 14px;
  color: #909399;
  margin: 0;
}

/* 统计概览 */
.stats-overview {
  margin-bottom: 32px;
}

.stats-card {
  border-radius: 12px;
  border: none;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.stats-item {
  display: flex;
  align-items: center;
  padding: 20px;
}

.stats-icon {
  font-size: 36px;
  margin-right: 20px;
}

.stats-icon.primary { color: #1890ff; }
.stats-icon.success { color: #52c41a; }
.stats-icon.warning { color: #fa8c16; }
.stats-icon.info { color: #8c8c8c; }

.stats-content {
  flex: 1;
}

.stats-value {
  font-size: 28px;
  font-weight: bold;
  color: #262626;
  margin-bottom: 5px;
}

.stats-label {
  font-size: 14px;
  color: #8c8c8c;
}

/* 系统状态区域 */
.system-status {
  margin-bottom: 32px;
}

.section-title {
  font-size: 18px;
  color: #262626;
  margin-bottom: 20px;
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
}

.status-card {
  border-radius: 12px;
  border: none;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.status-item {
  display: flex;
  align-items: center;
  padding: 20px;
}

.status-icon {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 16px;
  font-size: 24px;
}

.status-icon.success {
  background: #f6ffed;
  color: #52c41a;
}

.status-icon.warning {
  background: #fff7e6;
  color: #fa8c16;
}

.status-icon.info {
  background: #f0f5ff;
  color: #1890ff;
}

.status-content {
  flex: 1;
}

.status-content h3 {
  font-size: 16px;
  color: #262626;
  margin: 0 0 4px 0;
  font-weight: 600;
}

.status-text {
  font-size: 14px;
  margin: 0 0 8px 0;
  font-weight: 500;
}

.status-text.success {
  color: #52c41a;
}

.status-text.warning {
  color: #fa8c16;
}

.status-text.error {
  color: #ff4d4f;
}

.status-metrics {
  display: flex;
  gap: 16px;
}

.status-metrics span {
  font-size: 12px;
  color: #8c8c8c;
  background: #f5f5f5;
  padding: 2px 8px;
  border-radius: 4px;
}

/* 快速操作区域 */
.quick-actions {
  margin-bottom: 32px;
}

.quick-btn {
  width: 100%;
  height: 48px;
  font-size: 14px;
  border-radius: 8px;
  transition: all 0.3s ease;
}

.quick-btn:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .dashboard-container {
    padding: 0;
  }
  
  .page-title {
    font-size: 20px;
  }
  
  .stats-item {
    padding: 16px;
  }
  
  .stats-icon {
    font-size: 28px;
    margin-right: 16px;
  }
  
  .stats-value {
    font-size: 24px;
  }
  
  .status-item {
    padding: 16px;
  }
  
  .status-icon {
    width: 40px;
    height: 40px;
    margin-right: 12px;
    font-size: 20px;
  }
}
</style>



